<template>
  <div class="container">
    <div class="left">
      <!-- 新增 -->
      <Add @add="add" :studentData="studentData" />
      <!-- 修改 -->
      <Change @change="change" />
      <!-- 搜索 -->
      <Search @search="search" />
    </div>
    <div class="right">
      <!-- 内容 -->
      <Content :searchData="searchData" @deleteStudent="deleteStudent" />
    </div>
  </div>
</template>

<script>
import Change from "./Change.vue";
import Add from "./Add.vue";
import Search from "./SearchStudent.vue";
import Content from "./Content.vue";

export default {
  components: {
    Change,
    Add,
    Search,
    Content
  },
  data() {
    return {
      studentData: [
        { id: 1, name: "辉哥", age: "20", gender: "男" },
        { id: 2, name: "卢克", age: "30", gender: "男" },
        { id: 3, name: "小运", age: "40", gender: "女" },
        { id: 4, name: "小付", age: "50", gender: "女" }
      ],
      searchValue: "",
      searchType: "name"
    };
  },
  methods: {
    //   新增
    add(data) {
      // console.log(data);
      this.studentData.push(data);
    },
    // 删除
    deleteStudent(id) {
      // console.log(id);
      this.studentData = this.studentData.filter((v) => v.id != id);
    },
    //修改
    change(data) {
      // console.log(data);
      this.studentData = this.studentData.map((v) => {
        if (v.id == data.id) v = { ...data };
        return v;
      });
    },
    // 搜索
    search(type, value) {
      console.log(type, value);
      this.searchValue = value;
      this.searchType = type;
    }
  },
  computed: {
    //   搜索
    searchData() {
      return this.studentData.filter((v) => {
        return v[this.searchType].indexOf(this.searchValue) != -1;
      });
    }
  },
  mounted() {
    this.studentData[0].hhh = 123;
    // this.$set(this.studentData[0], "hhh", 123);
    console.log(this.studentData[0]);
  }
};
</script>

<style lang="scss">
.container {
  width: 900px;
  display: flex;
  .right {
    width: 600px;
    table {
      width: 100%;
      text-align: center;
    }
  }
  .left {
    width: 300px;
    background-color: #eeeeee;
    margin-right: 10px;
    div {
      background-color: #fff;
      width: 90%;
      height: 170px;
      margin: 0 auto 10px;
      padding: 0 5px;
      button {
        width: 100%;
        height: 30px;
      }
    }
  }
}
</style>
